<?php

namespace app\wechat\controller;

use think\Controller;
use think\Db;
use think\Request;

class Sign extends Controller
{
    protected $table_user_sms_info = 'user_sms_info';         /* 验证码信息表 */
    protected $table_user_bind_info = 'user_bind_info';       /* 绑定手机号 */
    protected $table_accounts_info = 'accounts_info';         /* 账户表 */
    protected $table_user_gift_record = 'user_gift_record';   /* 用户兑换表 */
    protected $table_user_score_log = 'user_score_log';       /*用户金币记录表*/
    protected $table_user_prop_log = 'user_prop_log';         /*用户道具记录表*/
    protected $table_user_mail_info = 'user_mail_info';        /*用户道具记录表*/
    protected function Db_connect_user_sms_info()
    {
        $user_sms_info = Db::connect('db_qpplatformdb')->table($this->table_user_sms_info);
        return $user_sms_info;
    }
    protected function Db_connect_user_gift_record()
    {
        $user_gift_record = Db::connect('db_qpplatformdb')->table($this->table_user_gift_record);
        return $user_gift_record;
    }
    protected function Db_connect_user_mail_info()
    {
        $user_mail_info = Db::connect('db_qpplatformdb')->table($this->table_user_mail_info);
        return $user_mail_info;
    }
    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_bind_info()
    {
        $user_bind_info = Db::connect('db_config2')->table($this->table_user_bind_info);
        return $user_bind_info;
    }
    protected function Db_connect_table_accounts_info()
    {
        $accounts_info = Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_score_log()
    {
        $user_score_log = Db::connect('db_config3')->table($this->table_user_score_log);
        return $user_score_log;
    }
    protected function Db_connect_user_prop_log()
    {
        $user_prop_log = Db::connect('db_config3')->table($this->table_user_prop_log);
        return $user_prop_log;
    }
    public function index()
    {
        return  $this->fetch();
    }
    function sender()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $UserID = $this->Db_connect_table_accounts_info()->where('NickName', $data['nickname'])->value('UserID');
            /* dump($UserID);*/
            if ($UserID) {
                $phone = $this->Db_connect_user_bind_info()->field('UserID')->where('UserID', $UserID)->value('Phone');
                if ($phone) {
                    $yesoldtoday = date('Y-m-d H:i:s', strtotime(date("Y-m-d")));
                    $today = date('Y-m-d H:i:s', strtotime(date("Y-m-d", time())) + 24 * 3600);
                    $arr_today = [$yesoldtoday, $today];
                    $map['InsertTime']    = ['between', $arr_today];
                    $map['Title']    = '每日签到奖励';
                    $mail_info = $this->Db_connect_user_mail_info()->where('UserID', $UserID)->where($map)->find();
                    if (empty($mail_info)) {
                        $data_arr['UserID'] = $UserID;
                        $data_arr['Status'] = 0;
                        $data_arr['Title'] = '每日签到奖励';
                        $data_arr['Content'] = '每日签到奖励,用户' . $UserID;
                        $data_arr['InsertTime'] = date('Y-m-d H:i:s', time());
                        $data_arr['Attach'] ='[[10001,5],[10006,5],[10003,5],[0,10000]]';
                        $re = $this->Db_connect_user_mail_info()->insert($data_arr);
                        if ($re) {
                            $result['code'] = 1;
                            $result['msg'] = '签到成功';
                            return json($result);
                            exit;
                        }
                    } else {
                        $this->error('今日已签到');
                    }
                } else {
                    $result['code'] = 2;
                    $result['msg'] = '未绑定手机号';
                    return json($result);
                    exit;
                }
            } else {
                $this->error('该账户信息不存在');
            }
        }
    }
}
